<?php
require_once "lib/nusoap.php";

$my_url = 'http://localhost/fashion_services/';
$hostname="1.234.91.160";
$username="cam";
$password="5678";
$databasename="fashion";

$api_key = "AIzaSyDgg-51NQtsv7MgITUGYAnyODIDhy9pcWY";
//echo date("Y-m-d");

class GCMResult {
	public $multicast_id;
	public $success;
	public $failure;
	public $canonical_ids;
	public $results;
	
	public function __construct($json) {
		$obj = json_decode($json, TRUE);
		$this->multicast_id = $obj['multicast_id'];
		$this->success = $obj['success'];
		$this->failure = $obj['failure'];
		$this->canonical_ids = $obj['canonical_ids'];
		$this->results = $obj['results'];
	}
}

class Notify {
	public $id;
	public $id_mem;
	public $id_sender;
	public $uid_sender;
	public $name_sender;
	public $id_owner;
	public $uid_owner;
	public $name_owner;
	public $content;
	public $id_post;
	public $image;
	public $type;
	public $cm_type;
	public $unread;
	public $date;
	public $status;
	
	public function __construct($id, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cm_type, $unread, $date, $status) {
		$this->id = $id;
		$this->id_mem = $id_mem;
		$this->id_sender = $id_sender;
		$this->uid_sender = $uid_sender;
		$this->name_sender = $name_sender;
		$this->id_owner = $id_owner;
		$this->uid_owner = $uid_owner;
		$this->name_owner = $name_owner;
		$this->content = $content;
		$this->id_post = $id_post;
		$this->image = $image;
		$this->type = $type;
		$this->cm_type = $cm_type;
		$this->unread = $unread;
		$this->date = $date;
		$this->status = $status;
	}
}



function LogInMember($user, $pass, $country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$user=urldecode($user);
	$result=mysql_query("SELECT * FROM member m WHERE (m.mem_id='$user' AND m.mem_pswd='$pass' AND m.status='1' AND m.`mem_area`='$country')");


	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return json_encode($r);
	}else{
		mysql_close($conn);
		return '{}';
	}
}

function GetInfoMember($user) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$user=urldecode($user);
	$result=mysql_query("SELECT * FROM member m WHERE (m.mem_id='$user' AND m.status='1')");



	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return  json_encode($r);
	}else{
		mysql_close($conn);
		return  '{}';
	}
}

function ChangePasswordMember($user, $passOld, $passNew) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$user=urldecode($user);
	$result=mysql_query("UPDATE member SET mem_pswd='$passNew' WHERE (mem_id='$user' AND mem_pswd='$passOld' AND status='1')");

	$rs=mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function CreateNewMember($user, $pass, $alias,$pic, $profile, $country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$user=urldecode($user);
	$alias=urldecode($alias);
	$profile=urldecode($profile);

	$check=mysql_query("SELECT * FROM member m WHERE (m.mem_id='$user' OR m.`mem_name`='$user') AND m.`mem_area`='$country'");
	if(mysql_num_rows($check)>0){
		mysql_close($conn);
		return  '-1';
	}else{
		$result=mysql_query("INSERT INTO `member` (`mem_id`, `mem_pswd`, `mem_name`, `mem_pic`, `mem_profile`, `mem_joinday`, `status`,`mem_area`) VALUES ('$user', '$pass', '$alias', '$pic', '$profile', NOW(), '1','$country')");
		$rs=mysql_affected_rows();
		mysql_close($conn);
		return $rs;
	}
}

function GetFashionDay($day, $country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	//include '../connect/common_start.php';
	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$sql="SELECT p.* FROM post p, store s WHERE  p.date='$NewDate' AND p.type='1' AND p.id_store=s.id AND s.`status`=1 AND s.store_area='$country'";
	//$result=mysql_query("SELECT * FROM post m WHERE (m.date='$NewDate' AND m.status='1')");
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	//include '../connect/common_end.php';
	mysql_close($conn);
	$rs=json_encode($rows);
	return $rs;
}

function GetListProduct($idPost) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$result=mysql_query("SELECT * FROM product p WHERE (p.id_post='$idPost' AND p.status='1')");

	$row=array();
	while ($r=mysql_fetch_assoc($result)) {
		$row[] = $r;
	}
	mysql_close($conn);
	return  json_encode($row);
}

function GetLikeMember($mem_id,$idPost) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$result=mysql_query("SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post='$idPost'");
	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return  json_encode($r);
	}else{
		mysql_close($conn);
		return  '{}';
	}
}

function SetLikePost($mem_id, $post_id, $type) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="UPDATE `post` SET `likes`=`likes`+'$type' WHERE (`id`='$post_id' AND status='1')";

	$result1=mysql_query($sql);
	if(mysql_affected_rows()>0)
	{
		$result=mysql_query("INSERT INTO `like` (`id_mem`, `id_post`, `like`, `datetime`) VALUES ('$mem_id', '$post_id', '$type', NOW())");
	}
	$rs=mysql_affected_rows();
	mysql_close($conn);
	return $rs;
}

function GetListCommentPost($post_id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$result=mysql_query("SELECT c.id,c.id_mem,c.id_post,c.cm_id,c.cm_content,c.date,m.mem_id as'mem_id',m.mem_name,m.mem_pic FROM `comment` c JOIN member m ON c.id_mem=m.id WHERE (c.id_post='$post_id' AND c.status='1') ORDER BY c.date");

	$row=array();
	while ($r=mysql_fetch_assoc($result)) {
		$row[]=$r;
	}
	mysql_close($conn);
	return json_encode($row);
}

function AddCommentPost($mem, $idPost, $cmid, $cmcontent) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$cmid=urldecode($cmid);
	$cmcontent=urldecode($cmcontent);
	$sql="UPDATE `post` SET `comments`=`comments`+'1' WHERE (`id`='$idPost' AND status='1')";
	$result1=mysql_query($sql);
	if(mysql_affected_rows()>0) {
		$result=mysql_query("INSERT INTO `comment` (`id_mem`, `id_post`, `cm_id`, `cm_content`, `date`, `status`) VALUES ('$mem', '$idPost', '$cmid', '$cmcontent',NOW(),'1')");
		$rs= mysql_insert_id();
		mysql_close($conn);
		return $rs;
	} else {
		mysql_close($conn);
		return 0;
	}
	
}

function CreateNewPost($mem_id, $year, $month, $day, $title, $price, $link, $image, $content) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$date = date_create($year.'-'.$month.'-'.$day);
	//$date= date_format($date, 'Y-m-d');
	$date = $year.'-'.$month.'-'.$day;
	//echo $date;
	$title=urldecode($title);
	$price=urldecode($price);
	$link=urldecode($link);
	$content=urldecode($content);
	$ip=$_SERVER['REMOTE_ADDR'];
	$sql="INSERT INTO `post` (`id_store`, `date`, `title`, `price`, `link`, `image`, `content`, `create_day`, `status`, `likes`, `comments`, `type`,`ip`) VALUES ('$mem_id', '$date', '$title', '$price', '$link', '$image', '$content', NOW(), '1','0','0' ,'2','$ip')";
	//echo $sql;
	$result=mysql_query($sql);
	if (!$result) {
		mysql_close($conn);
		return -1;
	}	
	$rs= mysql_insert_id();
	mysql_close($conn);
	return $rs;
}

function GetInfoStore($store_id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$result=mysql_query("SELECT * FROM store s WHERE (s.id='$store_id' AND s.status!='0')");
	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return  json_encode($r);
	}else{
		mysql_close($conn);
		return  '{}';
	}
}

function GetInfoFriend($mem_id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="SELECT f.id, f.create_date, f.type,m.id as friendid, m.mem_id, m.mem_name, m.mem_pic, m.mem_profile FROM friend f INNER JOIN member m ON f.mem_friend=m.id WHERE f.mem_id='$mem_id' AND f.`status`='1';";
	$result=mysql_query($sql);
	$row=array();
	while ($r=mysql_fetch_assoc($result))
	{
		$row[]=$r;
	}
	mysql_close($conn);
	return  json_encode($row);

}

function AddFriend($mem_id, $mem_id_friend) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="INSERT INTO `friend` (`mem_id`, `mem_friend`, `type`, `status`, `create_date`) VALUES ('$mem_id', '$mem_id_friend','1', '1', NOW()), ('$mem_id_friend', '$mem_id','0', '1', NOW())";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function DeleteFriend($mem_id, $mem_id_friend) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="DELETE FROM friend WHERE (friend.mem_id='$mem_id' AND friend.mem_friend='$mem_id_friend') OR (friend.mem_id='$mem_id_friend' AND friend.mem_friend='$mem_id')";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function GetFashionMember($mem_id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$result=mysql_query("SELECT * FROM post m WHERE (m.id_store='$mem_id')");

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetFashionDayAllMember($day) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$day=$_REQUEST['day'];
	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$result=mysql_query("SELECT * FROM post m WHERE (m.type='2' AND m.date='$NewDate' AND m.status='1')");

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetLikePost($idPost) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_post='$idPost'";
	$result=mysql_query($sql);
	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return  json_encode($r);
	}else{
		mysql_close($conn);
		return  '{}';
	}

}

function UpdateProfileMember($mem_id,$mem_pass, $profile) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$mem_id=urldecode($mem_id);
	$profile=urldecode($profile);
	$sql="UPDATE `member` SET `mem_profile`='$profile' WHERE (`mem_id`='$mem_id' AND mem_pswd='$mem_pass' AND status='1' )";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function UpdateImageMember($mem_id,$mem_pass, $mem_pic) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$mem_id=urldecode($mem_id);
	$sql="UPDATE `member` SET `mem_pic`='$mem_pic' WHERE (`mem_id`='$mem_id' AND mem_pswd='$mem_pass' AND status='1' )";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function GetInfoMemberById($id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="SELECT * FROM member m WHERE (m.id='$id' AND m.status='1')";
	$result=mysql_query($sql);
	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return json_encode($r);
	}else{
		mysql_close($conn);
		return '{}';
	}
}

function AddNotification($id, $mem_id, $notification, $id_post) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$notification=urldecode($notification);
	$sql="INSERT INTO `notification` (`id`, `mem_id`, `notification`, `id_post`) VALUES ('$id', '$mem_id', '$notification','$id_post')";
	$result=mysql_query($sql);
	$rs=mysql_affected_rows();
	mysql_close($conn);
	return  $rs;
}


function DeleteNotification($id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="DELETE FROM `notification` WHERE (`id`='$id')";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return  $rs;
}

function GetFashionDayFriend($mem_id,$day) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$sql="SELECT * FROM post p INNER JOIN
							(SELECT f.mem_friend FROM  friend f 
							WHERE f.mem_id='$mem_id' AND f.`status`='1') as f 
							ON p.id_store=f.mem_friend
			WHERE p.date='$NewDate'";
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}

	mysql_close($conn);
	return  json_encode($rows);
}

function GetNotification($id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="SELECT * FROM `notification` WHERE (`mem_id`='$id')";
	$result=mysql_query($sql);

	$sql2="DELETE FROM `notification` WHERE (`mem_id`='$id')";
	$result2=mysql_query($sql2);

	$row=array();
	while ($r=mysql_fetch_assoc($result))
	{
		$row[]=$r;
	}

	mysql_close($conn);
	return  json_encode($row);
}

function GetCountry($ip) {

	$x = gethostbyname($ip);
	//echo $x."<br>";
	$ip_long=sprintf('%u',ip2long($x));
	//echo $ip_long."<br>";
	$row = 1;
	//include 'getCountry/getCountry.php';
	if (($handle = fopen("getCountry/GeoIPCountryWhois.csv", "r")) !== FALSE) {
		while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
			$num = count($data);
			// echo "<p> $num fields in line $row: <br /></p>\n";
			$row++;
			// for ($c=0; $c < $num; $c++) {

			//     echo $data[$c] . "<br />\n";

			// }
			if($data[2]<= $ip_long && $ip_long <= $data[3]){
				$rs= $data[4];
				break;
			}
		}
		fclose($handle);
	}
	return $rs;
}

function UpdateNameMember($mem_id,$mem_pass, $mem_name) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$mem_id=urldecode($mem_id);
	$mem_name=urldecode($mem_name);
	$sql="UPDATE `member` SET `mem_name`='$mem_name' WHERE (`mem_id`='$mem_id' AND mem_pswd='$mem_pass' AND status='1' )";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function UpdateMember($mem_id,$mem_pass, $mem_name, $mem_profile, $country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$mem_id=urldecode($mem_id);
	$mem_name=urldecode($mem_name);
	$mem_profile=urldecode($mem_profile);
	
	$rs = -1;
	$check=mysql_query("SELECT * FROM member m WHERE m.mem_id<>'$mem_id' AND (m.mem_id='$mem_name' OR m.`mem_name`='$mem_name') AND m.`mem_area`='$country'");
	if(mysql_num_rows($check)>0){
		mysql_close($conn);
		return  $rs;
	} else {
		$sql="UPDATE `member` SET `mem_name`='$mem_name',`mem_profile`='$mem_profile' WHERE (`mem_id`='$mem_id' AND mem_pswd='$mem_pass' AND status='1' )";
		$result=mysql_query($sql);
		$rs= mysql_affected_rows();
	}
	mysql_close($conn);
	return $rs;
}

function GetFashionDayLike($day, $country, $member) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	//include '../connect/common_start.php';
	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$member' AND l.id_post=p.id)) as islike FROM post p, store s WHERE  p.date='$NewDate' AND p.type='1' AND p.`status`=1 AND p.id_store=s.id AND s.`status`=1 AND s.store_area='$country'";
	//$result=mysql_query("SELECT * FROM post m WHERE (m.date='$NewDate' AND m.status='1')");
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	//include '../connect/common_end.php';
	mysql_close($conn);
	$rs=json_encode($rows);
	return $rs;
}

function GetFashionMemberLike($mem_id,$country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
	//	FROM post p WHERE (p.id_store='$mem_id' AND p.`status`=1)";
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.id_store='$mem_id' AND p.`status`=1 AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC";
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetFashionDayAllMemberLike($day,$mem_id,$country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$day=$_REQUEST['day'];
	//echo '1111'.$day;
	$curDay= date("Y-m-d");
	//echo '2222'.$curDay;
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));



	//echo '3333'.$NewDate;
	//$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
	//	FROM post p WHERE (p.type='2' AND p.date='$NewDate' AND p.status='1')";

	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.type='2' AND p.status='1' AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC";

	//echo $sql;
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	//echo $rows;
	mysql_close($conn);
	return json_encode($rows);
}


function GetFashionDayFriendLike($mem_id,$day,$country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	/*$sql="SELECT *,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike FROM post p INNER JOIN
	 (SELECT f.mem_friend FROM  friend f
	 WHERE f.mem_id='$mem_id' AND f.`status`='1') as f
	 ON p.id_store=f.mem_friend
	 WHERE p.date='$NewDate' AND p.`status`=1" ;*/
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
				FROM (post p INNER JOIN
							(SELECT f.mem_friend FROM  friend f 
							WHERE f.mem_id='$mem_id' AND f.`status`='1') as f 
							ON p.id_store=f.mem_friend),member m
			WHERE (p.`status`=1 AND p.`type`='2' AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC" ;
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}

	mysql_close($conn);
	return  json_encode($rows);
}

function TestUTF8($value) {
	$v=urldecode($value);
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="UPDATE `member` SET `mem_profile`='$v' WHERE (`mem_id`='hung' AND mem_pswd='hung' AND status='1' )";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
	//return  $v;
}

function GetFashionAllMemberLike($mem_id,$country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
	//	FROM post p WHERE (p.id_store='$mem_id')";
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.id_store='$mem_id' AND p.`status`=1 AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC";

	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function DeleteFashionMember($mem_id,$id_post) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$sql="UPDATE `post` SET `status`='2' WHERE (`id`='$id_post' AND `id_store`='$mem_id' AND `type`='2')";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return  $rs;
}

function UpdateFashionMember($mem_id,$id_post,$image,$content) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$curDay= date("Y-m-d");
	//$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$content=urldecode($content);
	//$sql="UPDATE `post` SET `date`='$curDay', `image`='$image', `content`='$content' WHERE (`id`='$id_post' AND `id_store`='$mem_id' AND `type`='2')";
	$sql="UPDATE `post` SET `date`='$curDay',`create_day`=NOW(), `image`='$image', `content`='$content' WHERE (`id`='$id_post' AND `id_store`='$mem_id' AND `type`='2' AND `status`='1')";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return  $rs;
}

function GetFashionMemberAndMyLike($mem_id,$my_mem_id,$country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
	//	FROM post p WHERE (p.id_store='$mem_id' AND p.`status`=1)";
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$my_mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.id_store='$mem_id' AND p.`status`=1 AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC";
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetPostLikeMember($mem_id, $country, $start, $count) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql = "SELECT		p.*,
						IFNULL((SELECT	SUM(l.`like`)
								FROM	`like` l, `member` m
								WHERE	l.`id_mem`=m.`id`	AND
										m.`id`=$mem_id		AND
										l.`id_post`=p.`id`	AND
										m.`mem_area`='$country'), 0)	AS `islike`
			FROM		`post` p
			WHERE		p.`status`=1
			HAVING		`islike`>0
			ORDER BY	p.`create_day`	DESC
			LIMIT 		$start, $count;";
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetFashionDayAllMemberLikePage($day,$mem_id,$country,$start,$end) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.type='2' AND p.status='1' AND p.id_store=m.id AND m.mem_area='$country')
		 ORDER BY p.create_day DESC
		  LIMIT $start,$end";
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	//echo $rows;
	mysql_close($conn);
	return json_encode($rows);
}

function GetFashionDayLikePage($day, $country, $member,$start,$end) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	//include '../connect/common_start.php';
	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$member' AND l.id_post=p.id)) as islike
			FROM post p, store s WHERE  (p.date='$NewDate' AND p.type='1' AND p.`status`=1 AND p.id_store=s.id AND s.`status`=1 AND s.store_area='$country')
			 ORDER BY p.create_day ASC 
			 LIMIT $start,$end";
	//$result=mysql_query("SELECT * FROM post m WHERE (m.date='$NewDate' AND m.status='1')");
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	//include '../connect/common_end.php';
	mysql_close($conn);
	$rs=json_encode($rows);
	return $rs;
}

function GetFashionDayFriendLikePage($mem_id,$day,$country,$start,$end) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	/*$sql="SELECT *,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike FROM post p INNER JOIN
	 (SELECT f.mem_friend FROM  friend f
	 WHERE f.mem_id='$mem_id' AND f.`status`='1') as f
	 ON p.id_store=f.mem_friend
	 WHERE p.date='$NewDate' AND p.`status`=1" ;*/
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
				FROM (post p INNER JOIN
							(SELECT fr.mem_friend FROM  friend fr 
							WHERE fr.mem_id='$mem_id' AND fr.`status`='1' AND fr.`type`=1) as f 
							ON p.id_store=f.mem_friend),member m
			WHERE (p.`status`=1 AND p.`type`='2' AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC LIMIT $start,$end" ;
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}

	mysql_close($conn);
	return  json_encode($rows);
}

function GetFashionMemberAndMyLikePage($mem_id,$my_mem_id,$country,$start,$end) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
	//	FROM post p WHERE (p.id_store='$mem_id' AND p.`status`=1)";
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$my_mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.id_store='$mem_id' AND p.`status`=1 AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC LIMIT $start,$end";
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetThumbnailFashion($id_store,$day,$countday) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	// $sDay= date("Y-m-d");
	//$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	// $curDate = date('Y-m-d', strtotime($sDay . "$day days"));
	//echo $curDate;
	// $oldDate = date('Y-m-d', strtotime($curDate . "$countday days"));
	//echo $oldDate;
	// $sql="SELECT * FROM post p
		// WHERE p.id_store='$id_store' AND p.`status`='1' AND p.date<'$curDate' AND p.date>='$oldDate'
		// ORDER BY p.date ASC";
		$date = date("Y-m-d", strtotime($day));
	$sql="SELECT * FROM post p
		WHERE p.id_store='$id_store' AND p.`status`='1' AND p.date<'$date'
		GROUP BY p.date
		ORDER BY p.date DESC LIMIT $countday";
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}


function GetPostByID($idpost, $idmem, $country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT	p.*,
					(	SELECT 	SUM(l.`like`) as total 
						FROM 	`like` l 
						WHERE 	l.id_mem='$mem_id' 	AND 
								l.id_post=p.id) as islike
			FROM 	post p
			WHERE	p.`id`=$idpost";
			
	$rs1 = mysql_query($str);
	$rs2 = new stdClass();
	
	if (mysql_num_rows($rs1) > 0)
		$rs2 = mysql_fetch_assoc($rs1);
		
	mysql_close($conn);
	return json_encode($rs2);
}

function SetGCMID($userid, $gcmid) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "UPDATE	`member`
			SET		`gcm_id`='$gcmid'
			WHERE	`id`=$userid;";
	$result1 = mysql_query($str);
	
	mysql_close($conn);
	return ($result1) ? 1 : 0;
}

function SendNotification($message, $registrationIDs) {
	global $api_key;
	
	// Set POST variables
	$url = 'https://android.googleapis.com/gcm/send';
	
	$fields = array(
					'registration_ids'  => $registrationIDs,
					'data'              => array( "message" => $message ),
					);

	$headers = array( 
					'Authorization: key=' . $api_key,
					'Content-Type: application/json'
					);

	// Open connection
	$ch = curl_init();

	// Set the url, number of POST vars, POST data
	curl_setopt( $ch, CURLOPT_URL, $url );

	curl_setopt( $ch, CURLOPT_POST, true );
	curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

	curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

	curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
	// Execute post
	$result = curl_exec($ch);

	// Close connection
	curl_close($ch);

	echo $result;
}

function SendPendingNotifications($userid) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		n.*, m.`gcm_id`
			FROM		`notify` n
			INNER JOIN	`member` m
			ON			m.`id`=n.`id_mem`
			WHERE		n.`id_mem`=$userid	AND	
						n.`unread`=1
			ORDER BY	n.`date`	DESC;";
			
	$rs1 = mysql_query($str);
	if (mysql_num_rows($rs1)<1) {
		mysql_close($conn);
		return;
	}
	
	while ($r = mysql_fetch_assoc($rs1)) {
		$gcm = array();
		$id = $r['id'];
		$gcm[] = $r['gcm_id'];
		$idmem = $r['id_mem'];
		$idsender = $r['id_sender'];;
		$uidsender = $r['uid_sender'];;
		$namesender = $r['name_sender'];;
		$uidowner = $r['uid_owner'];;
		$nameowner = $r['name_owner'];;
		$content = $r['content'];;
		$idpost = $r['id_post'];;
		$image = $r['image'];;
		$type = $r['type'];;
		$unread = $r['unread'];;
		$date = $r['date'];;
		$obj = new Notify($id, $idmem, $idsender, $uidsender, $namesender, $uidowner, $nameowner, $content, $idpost, $image, $type, $unread, $date);
		$result5 = json_encode($obj);
		
		SendNotification($obj, $gcm);
	}
}



function NotifyComment($idcomment) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		c.`cm_content`	AS 	`content`,
						m.`id`			AS	`id_mem`,
						m.`gcm_id`,
						m1.`id`			AS	`id_sender`,
						m1.`mem_id`		AS	`uid_sender`,
						m1.`mem_name`	AS	`name_sender`,
						m1.`mem_pic`	AS	`image`,
						m2.`id`			AS	`id_owner`,
						m2.`mem_id`		AS	`uid_owner`,
						m2.`mem_name`	AS	`name_owner`,
						p.`id`			AS	`id_post`,
						c.`date`		AS	`date`
			FROM		`comment` c
			INNER JOIN	`post` p
			ON			(p.`id`=c.`id_post`	AND	p.`type`=2)
			INNER JOIN	`comment` c1
			ON			(c1.`id_post`=c.`id_post`	OR	c1.`id_mem`=p.`id_store`)
			INNER JOIN	`member` m
			ON			m.`id`=c1.`id_mem`
			INNER JOIN	`member` m1
			ON			m1.`id`=c.`id_mem`
			INNER JOIN	`member` m2
			ON			m2.`id`=p.`id_store`
			WHERE		c.`id`=$idcomment
			GROUP BY	m.`id`
			HAVING		m.`id`<>m1.`id`;";
			
	$rs1 = mysql_query($str);
	if (mysql_num_rows($rs1)>0) {
		while ($r = mysql_fetch_assoc($rs1)) {
			$id_mem = $r['id_mem'];
			$id_sender = $r['id_sender'];
			$uid_sender = $r['uid_sender'];
			$name_sender = $r['name_sender'];
			$id_owner = $r['id_owner'];
			$uid_owner = $r['uid_owner'];
			$name_owner = $r['name_owner'];
			$content = $r['content'];
			$id_post = $r['id_post'];
			$image = $r['image'];
			$type = ($r['id_mem']==$r['id_owner']) ? 1 : 2;
			$unread = 1;
			$date = $r['date'];
			$status = 1;
			
			$str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
					VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '0', '$unread', '$date', '$status');";
					
			$rs2 = mysql_query($str);
			if ($rs2==TRUE) {
				$ids = array();
				$ids[] = $r['gcm_id'];
				$id1 = mysql_insert_id();
				$notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
				$mess = json_encode($notify);
				SendNotification($mess, $ids);
			}
		}
	}
	
	mysql_close($conn);
	return 1;
}



function NotifyFriendPost($idpost) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		m.`id`			AS	`id_mem`,
						m.`gcm_id`,
						m1.`id`			AS	`id_sender`,
						m1.`mem_id`		AS	`uid_sender`,
						m1.`mem_name`	AS	`name_sender`,
						m1.`mem_pic`	AS	`image`,
						m1.`id`			AS	`id_owner`,
						m1.`mem_id`		AS	`uid_owner`,
						m1.`mem_name`	AS	`name_owner`
						p.`create_date`	AS	`date`
			FROM		`post` p
			INNER JOIN 	`friend` f
			ON			f.`mem_id`=p.`id_store`
			INNER JOIN 	`member` m
			ON			m.`id`=f.`mem_friend`
			INNER JOIN 	`member` m1
			ON			m1.`id`=p.`id_store`
			WHERE		p.`id`=$idpost
			GROUP BY	m.`id`
			HAVING		m.`id`<>m1.`id`;";
			
	$rs1 = mysql_query($str);
	if (mysql_num_rows($rs1)>0) {
		while ($r = mysql_fetch_assoc($rs1)) {
			$id_mem = $r['id_mem'];
			$id_sender = $r['id_sender'];
			$uid_sender = $r['uid_sender'];
			$name_sender = $r['name_sender'];
			$id_owner = $r['id_owner'];
			$uid_owner = $r['uid_owner'];
			$name_owner = $r['name_owner'];
			$content = "";
			$id_post = $idpost;
			$image = $r['image'];
			$type = 3;
			$unread = 1;
			$date = $r['date'];
			$status = 1;
			
			$str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
					VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '0', '$unread', '$date', '$status');";
					
			$rs2 = mysql_query($str);
			if ($rs2==TRUE) {
				$ids = array();
				$ids[] = $r['gcm_id'];
				$id1 = mysql_insert_id();
				$notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
				$mess = json_encode($notify);
				SendNotification($mess, $ids);
			}
		}
	}
	
	mysql_close($conn);
	return 1;
}

function NotifyFriendAdd($idmem, $idfriend) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		m.`id`			AS	`id_mem`,
						m.`gcm_id`,
						m1.`id`			AS	`id_sender`,
						m1.`mem_id`		AS	`uid_sender`,
						m1.`mem_name`	AS	`name_sender`,
						m1.`mem_pic`	AS	`image`,
						m1.`id`			AS	`id_owner`,
						m1.`mem_id`		AS	`uid_owner`,
						m1.`mem_name`	AS	`name_owner`,
						f.`create_date`	AS	`date`
			FROM		(	SELECT	*
							FROM	`friend`
							WHERE	`mem_id`=$idfriend	AND
									`mem_friend`=$idmem	)	f
			INNER JOIN	`member` m
			ON 			m.`id`=f.`mem_friend`
			INNER JOIN	`member` m1
			ON			m1.`id`=f.`mem_id`
			GROUP BY	m.`id`
			HAVING		m.`id`<>m1.`id`;";
	
	$rs1 = mysql_query($str);
	if (mysql_num_rows($rs1)>0) {
		$r = mysql_fetch_assoc($rs1);
		$id_mem = $r['id_mem'];
		$id_sender = $r['id_sender'];
		$uid_sender = $r['uid_sender'];
		$name_sender = $r['name_sender'];
		$id_owner = $r['id_owner'];
		$uid_owner = $r['uid_owner'];
		$name_owner = $r['name_owner'];
		$content = "";
		$id_post = 0;
		$image = $r['image'];
		$type = 4;
		$unread = 1;
		$date = $r['date'];
		$status = 1;
		
		$str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
				VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '0', '$unread', '$date', '$status');";
				
		$rs2 = mysql_query($str);
		if ($rs2==TRUE) {
			$ids = array();
			$ids[] = $r['gcm_id'];
			$id1 = mysql_insert_id();
			$notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
			$mess = json_encode($notify);
			SendNotification($mess, $ids);
		}
	}
	
	mysql_close($conn);
	return 1;
}

function ReadNotification($idnoti) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "UPDATE	`notify`
			SET		`unread`=0
			WHERE	`id`=$idnoti;";
			
	$rs1 = mysql_query($str);
	$rs2 = mysql_affected_rows();
	mysql_close($conn);
	
	return $rs2;
}

function GetNotifications($idmem, $start, $count) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "SELECT		n.*
			FROM		`notify` n
			WHERE		n.`id_mem`=$idmem
			ORDER BY	n.`date`	DESC
			LIMIT		$start, $count;";
	$rs1 = mysql_query($str);
	if (mysql_num_rows($rs1)<=0) {
		mysql_close($conn);
		return;
	}
	
	$rs2 = array();
	while ($r = mysql_fetch_assoc($rs1)) 
		$rs2[] = $r;
	mysql_close($conn);
	return json_encode($rs2);
}


function NewDevideID($uid, $gcmid) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "INSERT INTO	`device_id`	(`device_id`, `gcm_id`)
			VALUES					('$uid', '$gcmid')
			ON DUPLICATE KEY UPDATE `gcm_id`='$gcmid';";
			
	$rs1 = mysql_query($str);
	mysql_close($conn);
	
	return ($rs1) ? 1 : 0;
}


/****************************************************************************
*						NEW SERVICE FUNCTION DEFINATION						*
****************************************************************************/
function GetFashion($idaccount, $dayindex, $country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	
}



if(isset($_REQUEST['action'])){
	if($_REQUEST['action']=="Send" ){
		SendNotification();
	}
	elseif ($_REQUEST['action']=="NotifyComment" ){
		$idcm = $_REQUEST['idcm'];
		NotifyComment($idcm);
	}
	elseif ($_REQUEST['action']=="NotifyFriendPost" ){
		$idcm = $_REQUEST['idcm'];
		NotifyFriendPost($idcm);
	}
	elseif ($_REQUEST['action']=="NotifyFriendAdd" ){
		$idcm = $_REQUEST['idcm'];
		$idfm = $_REQUEST['idfm'];
		NotifyFriendAdd($idcm, $idfm);
	}
	elseif ($_REQUEST['action']=="SendPendingNotifications" ){
		$idcm = $_REQUEST['idcm'];
		SendPendingNotifications($idcm);
	}
	elseif ($_REQUEST['action']=="GetNotifications" ){
		$idcm = $_REQUEST['idcm'];
		$start = $_REQUEST['start'];
		$count = $_REQUEST['count'];
		echo GetNotifications($idcm, $start, $count);
	}
}

$server = new soap_server();
$server->register("LogInMember");
$server->register("GetInfoMember");
$server->register("ChangePasswordMember");
$server->register("CreateNewMember");
$server->register("GetFashionDay");
$server->register("GetListProduct");
$server->register("GetLikeMember");
$server->register("SetLikePost");
$server->register("GetListCommentPost");
$server->register("AddCommentPost");
$server->register("CreateNewPost");
$server->register("GetInfoStore");
$server->register("GetInfoFriend");
$server->register("AddFriend");
$server->register("DeleteFriend");
$server->register("GetFashionMember");
$server->register("GetFashionDayAllMember");
$server->register("GetLikePost");
$server->register("UpdateProfileMember");
$server->register("UpdateImageMember");
$server->register("GetInfoMemberById");
$server->register("AddNotification");
$server->register("DeleteNotification");		//khong dung
$server->register("GetFashionDayFriend");
$server->register("GetNotification");
$server->register("GetCountry");				//Get country
$server->register("UpdateNameMember");
$server->register("UpdateMember");
$server->register("GetFashionDayLike");
$server->register("GetFashionMemberLike");
$server->register("GetFashionDayAllMemberLike");
$server->register("GetFashionDayFriendLike");
$server->register("GetFashionAllMemberLike");
$server->register("DeleteFashionMember");
$server->register("UpdateFashionMember");
$server->register("GetFashionMemberAndMyLike");
$server->register("GetPostLikeMember");
$server->register("GetFashionDayAllMemberLikePage");
$server->register("GetFashionDayLikePage");
$server->register("GetFashionDayFriendLikePage");
$server->register("GetFashionMemberAndMyLikePage");
$server->register("GetThumbnailFashion");
$server->register("TestUTF8");
$server->register("SetGCMID");
$server->register("NotifyComment");
$server->register("NotifyFriendPost");
$server->register("NotifyFriendAdd");
$server->register("SendPendingNotifications");
$server->register("GetNotifications");
$server->register("ReadNotification");
$server->register("NewDevideID");
$server->register("GetPostByID");

/********************************************************************
*							NEW SERVICE								*
********************************************************************/






if ( !isset( $HTTP_RAW_POST_DATA ) ) $HTTP_RAW_POST_DATA =file_get_contents( 'php://input' );
$server->service($HTTP_RAW_POST_DATA);